<template>
	<view class="wallet-pages">
		<view class="top-block">
			<view class="name-block">
				<image src="/static/image/today.png" class="name-item" v-if="tabActive===1">
				</image>
				<view style="width: 33%;position: absolute;height: 90rpx;z-index: 999;" @click="changeTab(1)">
				</view>
				<image src="/static/image/yesterday.png" class="name-item" v-if="tabActive===2">
				</image>
				<view style="width: 33%;position: absolute;height: 90rpx;margin-left: 33%;z-index: 999;"
					@click="changeTab(2)">
				</view>
				<image src="/static/image/customize.png" class="name-item" v-if="tabActive===3">
				</image>
				<view style="width: 33%;position: absolute;height: 90rpx;margin-left: 66%;z-index: 999;"
					@click="changeTab(3)">
				</view>
			</view>
			<view class="top-info">
				<view class="price-block" v-if="tabActive===1">
					<view class="price-item">
						<view class="price-title">今日订单</view>
						<view class="price">{{statisticalData.merchantTodayOrders||0}}</view>
						<view class="unit">单</view>
					</view>
					<view class="line"></view>
					<view class="price-item">
						<view class="price-title">今日收入</view>
						<view class="price">￥{{statisticalData.balance||0}}</view>
						<view class="unit">元</view>
					</view>
				</view>
				<view class="price-block" v-if="tabActive===2">
					<view class="price-item">
						<view class="price-title">昨日订单</view>
						<view class="price">{{statisticalData.merchantTodayOrders||0}}</view>
						<view class="unit">单</view>
					</view>
					<view class="line"></view>
					<view class="price-item">
						<view class="price-title">昨日收入</view>
						<view class="price">￥{{statisticalData.balance||0}}</view>
						<view class="unit">元</view>
					</view>
				</view>
				<view class="top-item" v-if="tabActive===3">
					<view class="date-range" @click="showDatePicker = true">
						<view class="date-picker">
							<text
								class="date-text">{{startDate && endDate ? `${startDate} 至 ${endDate}` : '请选择时间范围'}}</text>
							<view class="calendar-icon">
								<text class="iconfont icon-a-gengduo1"></text>
							</view>
						</view>
					</view>
					<view class="price-block">
						<view class="price-item">
							<view class="price-title">订单</view>
							<view class="price">{{statisticalData.merchantTodayOrders||0}}</view>
							<view class="unit">单</view>
						</view>
						<view class="line"></view>
						<view class="price-item">
							<view class="price-title">收入</view>
							<view class="price">￥{{price||0}}</view>
							<view class="unit">元</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- <view class="date-filter">
			<view class="date-range" @click="showDatePicker = true">
				<view class="date-picker">
					<text class="date-text">{{startDate && endDate ? `${startDate} 至 ${endDate}` : '请选择时间范围'}}</text>
					<view class="calendar-icon">
						<text class="iconfont icon-calendar"></text>
					</view>
				</view>
			</view>
		</view> -->
		<!-- 日期选择弹窗 -->
		<uni-popup ref="popup" type="center" @change="handlePopupChange">
			<view class="date-picker-popup">
				<view class="popup-header">
					<text class="popup-title">选择时间范围</text>
				</view>
				<view class="popup-content">
					<view class="date-selector">
						<view class="date-item">
							<text class="date-label">起始日期</text>
							<picker mode="date" :value="tempStartDate" @change="handleTempStartDateChange">
								<view class="picker-value">{{tempStartDate || '请选择'}}</view>
							</picker>
						</view>
						<view class="date-item">
							<text class="date-label">截止日期</text>
							<picker mode="date" :value="tempEndDate" @change="handleTempEndDateChange">
								<view class="picker-value">{{tempEndDate || '请选择'}}</view>
							</picker>
						</view>
					</view>
				</view>
				<view class="popup-footer">
					<view class="btn-group">
						<view class="btn reset" @click="handleReset">重置</view>
						<view class="btn confirm" @click="handleConfirm">确定</view>
					</view>
				</view>
			</view>
		</uni-popup>
		<!-- <view class="wallet-item">
			<view class="wallet-info">
				<view class="wallet-text">累计收入</view>
				<view class="wallet-price">￥{{price||0}}</view>
			</view>
			<image src="/static/image/mine/wallet.png" class="wallet-img" mode="widthFix"></image>
		</view> -->
		<view class="title-block">
			<view class="title-item">
				<view class="line"></view>
				<view class="title">订单列表</view>
			</view>
		</view>
		<template v-if="list.length>0">
			<view class="news-item" v-for="item in list" :key="item.id">
				<view class="news-info">
					<view class="news-title">收到车费</view>
					<view class="news-price">+￥{{item.transAmount}}</view>
				</view>
				<view class="time">{{item.createTime}}</view>
			</view>
		</template>
		<view v-if="more==false&&list.length==0" class="empty">暂无数据</view>
		<view v-else-if="more==false&&list.length>0" class="empty">没有更多了</view>
	</view>
</template>

<script>
	import {
		getIndexDataApi,
		getTravelRecordApi,
		getAccumulatedExpensesApi
	} from '@/api/home.js'
	import {
		isMore
	} from '@/util/util.js'
	export default {
		data() {
			return {
				conf: {
					pageNum: 1,
					pageSize: 10,
					startDate: '',
					endDate: ''
				},
				list: [],
				more: true,
				price: 0,
				startDate: '',
				endDate: '',
				showDatePicker: false,
				tempStartDate: '',
				tempEndDate: '',
				tabActive: 1,
				statisticalData: {},
			}
		},
		watch: {
			showDatePicker(val) {
				if (val) {
					this.$refs.popup.open()
					this.tempStartDate = this.startDate
					this.tempEndDate = this.endDate
				} else {
					this.$refs.popup.close()
				}
			}
		},
		methods: {
			changeTab(e) {
				this.tabActive = e
			},
			handlePopupChange(e) {
				if (!e.show) {
					this.showDatePicker = false
				}
			},
			handleTempStartDateChange(e) {
				this.tempStartDate = e.detail.value
			},
			handleTempEndDateChange(e) {
				this.tempEndDate = e.detail.value
			},
			handleReset() {
				this.tempStartDate = ''
				this.tempEndDate = ''
			},
			handleConfirm() {
				this.startDate = this.tempStartDate
				this.endDate = this.tempEndDate
				this.conf.startDate = this.tempStartDate ? `${this.tempStartDate} 00:00:00` : ''
				this.conf.endDate = this.tempEndDate ? `${this.tempEndDate} 23:59:59` : ''
				this.showDatePicker = false
				this.handleRefresh()
			},
			handleCancel() {
				this.showDatePicker = false
			},
			getCurrentDate() {
				const date = new Date();
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				return `${year}-${month}-${day}`;
			},
			getDetail() {
				const params = {
					startDate: this.conf.startDate,
					endDate: this.conf.endDate
				}
				getAccumulatedExpensesApi(params).then((res) => {
					this.price = res
				})
			},
			handleRefresh() {
				this.conf.pageNum = 1;
				this.more = true;
				this.getList(true);
				this.getDetail();
			},
			getList(refresh = false) {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getTravelRecordApi(this.conf).then(res => {
					let list = res.list || [];
					if (refresh) {
						this.list = list;
					} else {
						this.list = [...this.list, ...list]
					}
					this.more = isMore(this.conf.pageNum, this.conf.pageSize, res.total)
				})
			}
		},
		onReachBottom() {
			if (this.more) {
				this.conf.pageNum++;
				this.getList();
			}
		},
		onLoad() {
			this.getList(true);
			this.getDetail()
		}
	}
</script>

<style lang="scss" scoped>
	.wallet-pages {
		padding: 30rpx;
		background: #f8f9fc;

		.top-block {
			margin-bottom: 30rpx;

			.name-block {
				display: flex;
				align-items: center;

				.name-item {
					height: 100rpx;
					width: 100%;
					display: flex;
					justify-content: center;
				}
			}

			.top-info {
				width: 100%;
				height: 300rpx;
				background: url('/static/image/bg-img.png') no-repeat center;
				background-size: 100%;
				border-radius: 0 0 30rpx 30rpx;
				margin-top: -30rpx;
				display: flex;
				align-items: center;

				.price-block {
					width: 100%;
					display: flex;
					align-items: center;
					color: #fff;

					.line {
						width: 4rpx;
						height: 36rpx;
						background: linear-gradient(#4582ff, #fff, #4582ff);
					}

					.price-item:first-child {
						width: 41%;
					}

					.price-item:last-child {
						width: 59%;
					}

					.price-item {
						display: flex;
						align-items: center;
						justify-content: center;

						.price-title {
							font-size: 32rpx;
							margin-right: 10rpx;
						}

						.price {
							font-size: 54rpx;
							font-weight: bold;
						}

						.unit {
							font-size: 32rpx;
							margin-left: 10rpx;
						}
					}
				}

				.top-item {
					width: 100%;
					display: flex;
					flex-direction: column;
					align-items: center;

					.date-range {
						width: calc(100% - 60rpx);
						display: flex;
						align-items: center;
						justify-content: space-between;
						background: #fff;
						border-radius: 24rpx;
						padding: 10rpx;
						box-shadow: 0 8rpx 24rpx rgba(49, 117, 255, 0.06);
						border: 2rpx solid rgba(49, 117, 255, 0.08);
						transition: all 0.3s ease;
						margin-bottom: 30rpx;

						&:active {
							transform: scale(0.98);
						}

						.date-picker {
							flex: 1;
							display: flex;
							align-items: center;
							justify-content: center;
							//background: rgba(49, 117, 255, 0.04);
							border-radius: 16rpx;
							padding: 24rpx 28rpx;
							margin: 0 10rpx;
							transition: all 0.3s ease;
							position: relative;
							overflow: hidden;
							min-width: 0;

							.date-text {
								font-size: 34rpx;
								color: #3175ff;
								font-weight: bold;
								position: relative;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
								flex: 1;
								text-align: center;

								&::after {
									content: '';
									position: absolute;
									bottom: -4rpx;
									left: 0;
									right: 0;
									height: 2rpx;
									background: #3175ff;
									opacity: 0.2;
									transform: scaleX(0.8);
									transform-origin: center;
									transition: all 0.3s ease;
								}

								&:hover::after {
									transform: scaleX(1);
									opacity: 0.4;
								}
							}

							.calendar-icon {
								flex-shrink: 0;
								display: flex;
								align-items: center;
								justify-content: center;
								// width: 48rpx;
								// height: 48rpx;
								// background: rgba(49, 117, 255, 0.08);
								// border-radius: 50%;
								//margin-left: 16rpx;
								transition: all 0.3s ease;

								&:active {
									background: rgba(49, 117, 255, 0.15);
									transform: scale(0.95);
								}

								.iconfont {
									font-size: 28rpx;
									font-weight: bold;
									color: #767676;
								}
							}
						}
					}
				}
			}
		}

		.date-filter {
			margin-bottom: 30rpx;

			.date-range {
				display: flex;
				align-items: center;
				justify-content: space-between;
				background: #fff;
				border-radius: 24rpx;
				padding: 28rpx 32rpx;
				box-shadow: 0 8rpx 24rpx rgba(49, 117, 255, 0.06);
				border: 2rpx solid rgba(49, 117, 255, 0.08);
				transition: all 0.3s ease;

				&:active {
					transform: scale(0.98);
				}

				.date-picker {
					flex: 1;
					display: flex;
					align-items: center;
					justify-content: center;
					background: rgba(49, 117, 255, 0.04);
					border-radius: 16rpx;
					padding: 24rpx 28rpx;
					margin: 0 10rpx;
					transition: all 0.3s ease;
					position: relative;
					overflow: hidden;
					min-width: 0;

					.date-text {
						font-size: 30rpx;
						color: #3175ff;
						font-weight: 500;
						position: relative;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						flex: 1;
						text-align: center;

						&::after {
							content: '';
							position: absolute;
							bottom: -4rpx;
							left: 0;
							right: 0;
							height: 2rpx;
							background: #3175ff;
							opacity: 0.2;
							transform: scaleX(0.8);
							transform-origin: center;
							transition: all 0.3s ease;
						}

						&:hover::after {
							transform: scaleX(1);
							opacity: 0.4;
						}
					}

					.calendar-icon {
						flex-shrink: 0;
						display: flex;
						align-items: center;
						justify-content: center;
						width: 48rpx;
						height: 48rpx;
						background: rgba(49, 117, 255, 0.08);
						border-radius: 50%;
						margin-left: 16rpx;
						transition: all 0.3s ease;

						&:active {
							background: rgba(49, 117, 255, 0.15);
							transform: scale(0.95);
						}

						.icon-calendar {
							font-size: 32rpx;
							color: #3175ff;
						}
					}
				}
			}
		}

		.wallet-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;
			height: 400rpx;
			padding: 30rpx;
			border-radius: 30rpx;
			background-color: #3175ff;
			margin-bottom: 30rpx;

			.wallet-info {
				.wallet-text {
					color: #fff;
					font-size: 30rpx;
					margin-bottom: 30rpx;
				}

				.wallet-price {
					color: #fff;
					font-size: 50rpx;
					font-weight: bold;
				}

				.wallet-btn {
					background-color: #fff;
					width: 140rpx;
					color: #3175ff;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 10rpx;
					font-size: 30rpx;
					height: 50rpx;
					margin-top: 50rpx;
				}
			}

			.wallet-img {
				width: 280rpx;
			}
		}

		.title-block {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 30rpx;
			padding: 0 10rpx;

			.title-item {
				display: flex;
				align-items: center;

				.line {
					width: 20rpx;
					height: 42rpx;
					background-color: #3175ff;
					border-radius: 60rpx;
					margin-right: 16rpx;
				}

				.title {
					font-weight: bold;
					font-size: 34rpx;
				}
			}
		}

		.news-item {
			padding: 30rpx;
			background-color: #fff;
			margin-bottom: 30rpx;
			border-radius: 30rpx;
		}


		.news-info {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 20rpx;

			.news-title {
				font-weight: bold;
				font-size: 32rpx;
			}

			.news-price {
				color: #24aa25;
				font-size: 34rpx;
				font-weight: bold;
			}
		}

		.time {
			font-size: 26rpx;
			color: #767676;
		}

		.date-picker-popup {
			width: 640rpx;
			background: #fff;
			border-radius: 24rpx;
			overflow: hidden;
			box-shadow: 0 12rpx 36rpx rgba(0, 0, 0, 0.08);

			.popup-header {
				padding: 36rpx;
				border-bottom: 2rpx solid #f5f5f5;
				background: #f8f9fc;

				.popup-title {
					font-size: 34rpx;
					font-weight: bold;
					color: #333;
					text-align: center;
				}
			}

			.popup-content {
				padding: 36rpx;

				.date-selector {
					.date-item {
						margin-bottom: 36rpx;

						&:last-child {
							margin-bottom: 0;
						}

						.date-label {
							font-size: 28rpx;
							color: #666;
							margin-bottom: 16rpx;
							display: block;
							font-weight: 500;
						}

						.picker-value {
							height: 88rpx;
							line-height: 88rpx;
							background: #f8f9fc;
							border-radius: 12rpx;
							padding: 0 24rpx;
							font-size: 30rpx;
							color: #333;
							transition: all 0.3s ease;
							border: 2rpx solid transparent;

							&:active {
								background: #f0f2f7;
								border-color: rgba(49, 117, 255, 0.2);
							}
						}
					}
				}
			}

			.popup-footer {
				padding: 24rpx 36rpx;
				border-top: 2rpx solid #f5f5f5;
				background: #f8f9fc;

				.btn-group {
					display: flex;
					justify-content: space-between;

					.btn {
						flex: 1;
						height: 88rpx;
						line-height: 88rpx;
						text-align: center;
						border-radius: 12rpx;
						font-size: 30rpx;
						margin: 0 12rpx;
						transition: all 0.3s ease;
						font-weight: 500;

						&.reset {
							background: #f0f2f7;
							color: #666;

							&:active {
								background: #e8e8e8;
								transform: scale(0.98);
							}
						}

						&.confirm {
							background: #3175ff;
							color: #fff;

							&:active {
								background: #2b6ae0;
								transform: scale(0.98);
							}
						}
					}
				}
			}
		}
	}
</style>